<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no"/>
<title>新增地址</title>
<link href="/Public/zjdailyCard/css/bkmain.css" type="text/css" rel="stylesheet" />
</head>
<body class="addressBg">
<!-------------注册失败---------------------->
<div class="uploadregstFail borderRadius">
    <p class="pFail"><span class="icon iconfont icon-tanhao"></span>激活成功！</p>
</div>
<!-------------注册失败------------------------>
 <!-------------登录---------------------->
<div class="uploadloginFail borderRadius" id="customerAddrEditTip">
    <p class="pFail"><span class="icon iconfont icon-tanhao"></span><span class="info"></span></p>
</div>
<!-------------登录失败------------------------>
<div class="main">
<div class="back"><a href="{:U('Wap/CustomerAddr/home', $bag['data_card'])}">返回首页</a></div>
<form>
<ul class="ulAddress ulAddEdit">
    <present name="address">
    <input type="hidden" name="id" value="{$address['id']}">
    <li>
        <div class="spanAddress addressAdd boxSize">
            <input placeholder="姓名" type="text" class="uploadTxt" name="username" id="userName" data-validate="nameFormat" data-times="0" value="{$address['username']}">
            <div class="tipinfo"><div class="error icon iconfont icon-iconfontgantanhao"></div><div class="valid icon iconfont icon-gou"></div></div>
        </div>   
    </li>
    <li>
        <div class="spanAddress addressAdd boxSize">
            <input placeholder="手机号码" type="tel" class="uploadTxt" name="telephone" id="telephone" data-validate="telephoneFormat" data-times="0" value="{$address['telephone']}">
             <div class="tipinfo"><div class="error icon iconfont icon-iconfontgantanhao"></div><div class="valid icon iconfont icon-gou"></div></div>
        </div>   
    </li>
     <li>
        <div class="spanAddress addressAdd boxSize">
            <select class="provience" name="provience">
                <option value="0">请选择</option>
            </select>
            <select class="city" name="city">
                <option value="0">请选择</option>
            </select>
            <select class="region" data-validate="requireSelects" name="region" data-times="0">
                <option value="0">请选择</option>
            </select>
            <div class="tipinfo">
                <div class="error icon iconfont icon-iconfontgantanhao"></div>
                <div class="valid icon iconfont icon-gou"></div>
            </div>
        </div>   
    </li>
    <li class="liTxt">
     	<div class="spanAddress spanFr addressAdd boxSize">
            <textarea placeholder="详细地址" class="txtUpload boxSize" name="address" id="detailDress" data-validate="requireLength" data-min="2" data-times="0">{$address['address']}</textarea>
            <div class="tipinfo"><div class="error icon iconfont icon-iconfontgantanhao"></div><div class="valid icon iconfont icon-gou"></div></div>
     	</div>
    </li>
    <else />
    <li>
        <div class="spanAddress addressAdd boxSize">
            <input placeholder="姓名" type="text" class="uploadTxt" name="username" id="userName" data-validate="nameFormat" data-times="-1" value="{$address['username']}">
            <div class="tipinfo"><div class="error icon iconfont icon-iconfontgantanhao"></div><div class="valid icon iconfont icon-gou"></div></div>
        </div>   
    </li>
    <li>
        <div class="spanAddress addressAdd boxSize">
            <input placeholder="手机号码" type="tel" class="uploadTxt" name="telephone" id="telephone" data-validate="telephoneFormat" data-times="-1" value="{$address['telephone']}">
             <div class="tipinfo"><div class="error icon iconfont icon-iconfontgantanhao"></div><div class="valid icon iconfont icon-gou"></div></div>
        </div>   
    </li>
     <li>
        <div class="spanAddress addressAdd boxSize">
            <select class="provience" name="provience">
                <option value="0">请选择</option>
            </select>
            <select class="city" name="city">
                <option value="0">请选择</option>
            </select>
            <select class="region" data-validate="requireSelects" name="region" data-times="-1">
                <option value="0">请选择</option>
            </select>
            <div class="tipinfo">
                <div class="error icon iconfont icon-iconfontgantanhao"></div>
                <div class="valid icon iconfont icon-gou"></div>
            </div>
        </div>   
    </li>
    <li class="liTxt">
     	<div class="spanAddress spanFr addressAdd boxSize">
            <textarea placeholder="详细地址" class="txtUpload boxSize" name="address" id="detailDress" data-validate="requireLength" data-min="2" data-times="-1">{$address['address']}</textarea>
            <div class="tipinfo"><div class="error icon iconfont icon-iconfontgantanhao"></div><div class="valid icon iconfont icon-gou"></div></div>
     	</div>
    </li>
    </present>
</ul>
<form>
<div class="btn ulAddEdit">
	<ul class="ulBtn">
    	<li><input value="保存" id="saveBtn" class="saveBtn borderRadius gray" type="button"></li>
        <li class="fr"><a href="{:U('Wap/CustomerAddr/home', $bag['data_card'])}" class="saveBtn fr backBtn boxSize borderRadius" id="customerAddrEditBack">返回</a></li>
    </ul>
</div>

<div class="divfooter mart100">
    <p>浙报新媒体技术支持</p>
    <p>联系方式：0571-85315689</p>
    <p class="pFooter">浙江日报新闻发展有限公司 | Copyright © 2014 - 2015</p>
</div>
</div>
<script type="text/javascript" src="/Public/zjdailyCard/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/Public/zjdailyCard/js/validateadd.js"></script>
<script type="text/javascript" src="/Public/zjdailyCard/js/offline.china.js"></script>
<script type="text/javascript">
    var editSuccessCallback = function( result ){
        //if( result.err == 200 ){
            //$( "#customerAddrEditTip" ).find( "span.info" ).html( result.msg );
            //$( "#customerAddrEditTip" ).show();
            setTimeout( function(){
                $( "#customerAddrEditTip" ).hide();

                window.location.href=$( "#customerAddrEditBack" ).attr( "href" );
            }, 800 );
            //window.location.href=$( "#customerAddrEditBack" ).attr( "href" );
        //}else{
        //}
    };
    var editFailCallback = function( result ){
            setTimeout( function(){
                $( "#customerAddrEditTip" ).hide();
                //window.location.href=$( "#customerAddrEditBack" ).attr( "href" );
            }, 800 );

    };
    var editErrorCallback = function(){
    };

    var editFormData = function(){
        var _data = {};
        $( "input" ).each( function(){
                _data[$(this).attr("name")] = $( this ).val();
        } );
        $( "textarea" ).each( function(){
                _data[$(this).attr("name")] = $( this ).val();
        } );
        $( "select" ).each( function(){
                _data[$(this).attr("name")] = $( this ).val();
        } );
        return _data;
    };

    $( "#saveBtn" ).on( "click", function(){
            var _data = editFormData();
            $.ajax( {
                url: "{:U('Wap/CustomerAddr/save')}",
                type: "POST",
                data: _data,
                success: function( result ){
                    //console.log( result );
                    $( "#customerAddrEditTip" ).find( "span.info" ).html( result.msg );
                    $( "#customerAddrEditTip" ).show();
                    if( result.err == 200 ){
                        editSuccessCallback( result );
                    }else{
                        editFailCallback( result );
                    }
                },
                error: function(){
                    editErrorCallback();
                }
            } )
    } );

    var successCallback=function(){
        $( "#saveBtn" ).attr( "disabled", false ).removeClass("gray");
    }
    var errorCallback=function(){
        $( "#saveBtn" ).attr( "disabled", true ).addClass("gray");
    }
    common.validate.init( successCallback, errorCallback );
</script>
<script type="text/javascript">
	var miniCity = ( function(){
		var _provience = undefined;
		var _city = undefined;
		var _region = undefined;

		var _changeProvience = function( code ){
			!code && ( code = $(this).val() );
			var options = undefined;
            if( code > 0 ){
                options = "<option value=0>请选择</option>";
                for( var i in offline.china.cities[code] ){
                    options += "<option value="+i+">"+ offline.china.cities[code][i] + "</option>";
                }
                _city.html("");
                _city.append( $(options) ).trigger( "change" );
            };
            /*(code > 0) && $.get( '/?m=user&c=miniCity&a=check', { code: code }, function(result){
				options = "<option value=0>请选择</option>";
				for( var i in result.sons ){
					options += "<option value="+result.sons[i].id+">"+result.sons[i].regionname + "</option>";
				}
				//_provience.html( "" );
				_city.html("");
				//_provience.append( $( "<option value="+result.parent.id+">" + result.parent.regionname + "</option>") );
				_city.append( $(options) ).trigger( "change" );
			});*/
            (code == 0) && _city.html( "<option value=0>请选择</option>" ).trigger("change");
		};
		var _changeCity = function( code, region ){
			!code && ( code = $(this).val() );
            if( code > 0 ){
				options = "<option value=0>请选择</option>";
                for( var i in offline.china.counties[code] ){
					options += "<option value="+i+">"+offline.china.counties[code][i] + "</option>";
				}
				_region.html("");
				_city.find( "option[value=" + code + "]" ).attr( 'selected', true );
				_region.append( $(options) );
				region && _region.find( "option[value=" + region + "]" ).attr( "selected", true ).trigger( "change" );
                region == undefined && _region.trigger( "change" ); 
            }
            /*( code > 0 ) && $.get( '/?m=user&c=miniCity&a=check', { code: code }, function(result){
				options = "<option value=0>请选择</option>";
				for( var i in result.sons ){
					options += "<option value="+result.sons[i].id+">"+result.sons[i].regionname + "</option>";
				}
				_region.html("");
				_city.find( "option[value=" + code + "]" ).attr( 'selected', true );
				//_city.find( "option[value=" + code + "]" ).prop( 'selected', true );
				_region.append( $(options) );
				region && _region.find( "option[value=" + region + "]" ).attr( "selected", true ).trigger( "change" );
                region == undefined && _region.trigger( "change" ); 
			});*/
            ( code == 0 ) &&  _region.html("<option value=0>请选择</option>").trigger( "change" );
		};
		var changeRegion = function( code ){
		};

        var _initProvience = function( code, selected ){
            /*$.post( '/?m=user&c=miniCity&a=checks', {codes: code}, function( result ){
                 var options = "<option value=0>请选择</option>";
                 for( var i in result ){
                    options += "<option value=" + result[i].id + ">" + result[i].regionname + "</option>"
                 }
                 _provience.html( "" );
                 _provience.html( options );
                 _provience.find( "option[value="+selected+"]" ).attr( "selected", true );
            });*/
            var options = "<option value=0>请选择</option>";
            for( var i in code ){
                options += "<option value=" + code[i] + ">" + offline.china.provinces[code[i]] + "</option>"
            }
            _provience.html( "" );
            _provience.html( options );
            _provience.find( "option[value="+selected+"]" ).attr( "selected", true );

        }
		return {
			init: function( s_provience, s_city, s_region, provience, city, region ){
				_provience = $( s_provience );
				_city = $( s_city );
				_region = $( s_region );
                _initProvience( [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35], provience );
                //_initProvience( [ 31 ], provience );
				provience && _changeProvience( provience );
				city && _changeCity( city, region );

                _provience.bind( "change", function(){
                        _changeProvience( $(this).val() );
                });                
				_city.bind( "change", function(){
					_changeCity( $(this).val() );
				});
			},
			//changeCity: _changeCity
		}
	})();
    miniCity.init( ".provience", ".city", ".region", "{$address['province']}", "{$address['city']}", "{$address['county']}" );
</script>
</body>
</html>
